package backpage.service.Impl;

import backpage.entity.SalaryLevel;
import backpage.exception.BusinessException;
import backpage.mapper.SalaryLevelMapper;
import backpage.service.SalaryLevelService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class SalaryLevelServiceImpl implements SalaryLevelService {
    private final SalaryLevelMapper salaryLevelMapper;

    @Override
    public List<SalaryLevel> getAllSalaryLevels() {
        return salaryLevelMapper.selectAll();
    }

    @Override
    public SalaryLevel getSalaryLevelById(Integer id) {
        return salaryLevelMapper.selectById(id);
    }



    @Override
    @Transactional
    public void updateSalaryLevel(SalaryLevel salaryLevel) {
        if (salaryLevelMapper.countByNameExcludeId(salaryLevel.getSalaryLevelName(), salaryLevel.getSalaryLevelId()) > 0) {
            throw new BusinessException("薪资级别名称已存在");
        }
        salaryLevelMapper.update(salaryLevel);
    }

    @Override
    @Transactional
    public void deleteSalaryLevel(Integer id) {
        // 检查是否有员工关联该薪资级别
        // if (employeeService.countBySalaryLevelId(id) > 0) {
        //     throw new BusinessException("该薪资级别已被使用，无法删除");
        // }
        salaryLevelMapper.deleteById(id);
    }

    @Override
    public boolean isLevelNameExist(String name, Integer excludeId) {
        return salaryLevelMapper.countByNameExcludeId(name, excludeId) > 0;
    }
}
